//
//  EveryDayCheckViewController.m
//  Qihuo
//
//  Created by Vencoo on 14-1-15.
//  Copyright (c) 2014年 Vencoo. All rights reserved.
//

#import "EveryDayCheckViewController.h"
#import "ParentCheckCell.h"
@interface EveryDayCheckViewController ()
{

}
@end

@implementation EveryDayCheckViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    self.checkURL = resultOfRCXJ;
    self.checkNames = @[@"检查情况",@"         "];
    [super viewDidLoad];
    self.title = @"日常巡检记录查询";
	// Do any additional setup after loading the view.
}
#pragma mark - UITableViewDataSource
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"cell";
    ParentCheckCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[ParentCheckCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
    }
    cell.floorNameLb.text = [self.chekckProcesses objectAtIndex:indexPath.row];
    cell.remarkLb.text = [self.checkResults objectAtIndex:indexPath.row];
    return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [[self.floorCheckDatas objectAtIndex:section]count];
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
    return cell.frame.size.height;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.floorNames.count;
}
//- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
//{
//    return [self.floorNames objectAtIndex:section];
//}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    NSIndexPath *index = [NSIndexPath indexPathForRow:0 inSection:0];
    UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:index];
    UILabel *label = [UILabel new];
    label.text = [self.floorNames objectAtIndex:section];
    label.font = [UIFont systemFontOfSize:20];
    CGSize cgSz = [label.text sizeWithFont:label.font constrainedToSize:CGSizeMake(MAXFLOAT, 40)];
    label.frame = CGRectMake((cell.frame.size.width - cgSz.width)/2, (cell.frame.size.height - cgSz.height)/2, cell.frame.size.width, cell.frame.size.height);
    label.textAlignment = 1;
    label.backgroundColor = [UIColor lightGrayColor];
    return label;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *footView = [UIView new];
    
    return footView;
}
#pragma mark - request
- (void)requestDone:(ASIFormDataRequest *)request
{
    [MBProgressHUD hideHUDForView:self.view animated:YES];
    NSString *response = [request responseString];
    NSDictionary *queryDic = [response JSONValue];
    NSDictionary *queryDataArr = [queryDic objectForKey:@"data"];
    NSArray *floorData = [queryDataArr objectForKey:@"floorData"];
    
    if ([[floorData objectAtIndex:0] valueForKey:@"floorName"])
    {
        self.floorNames = [NSMutableArray array];
    }
    if ([[floorData objectAtIndex:0] valueForKey:@"floorRemark"])
    {
        self.floorRemarks = [NSMutableArray array];
    }
    for (int i = 0; i < floorData.count; i ++)
    {
        NSDictionary *queryDic = [floorData objectAtIndex:i];
        if ([queryDic objectForKey:@"floorName"]) {
            [self.floorNames addObject:[queryDic valueForKey:@"floorName"]];
        }
        if ([queryDic objectForKey:@"checkData"]) {
            [self.floorCheckDatas addObject:[queryDic valueForKey:@"checkData"]];
        }
    }
    for (int i = 0; i < self.floorCheckDatas.count; i ++)
    {
        NSArray *floorDatas = [self.floorCheckDatas objectAtIndex:i];
        NSMutableArray *process = [NSMutableArray array];
        NSMutableArray *results = [NSMutableArray array];
        for (int j = 0; j < floorDatas.count; j ++)
        {
            NSDictionary *floorDic = [floorDatas objectAtIndex:j];
            if ([floorDic objectForKey:@"process"]) {
                [process addObject:[floorDic objectForKey:@"process"]];
            }
            if ([floorDic objectForKey:@"result"]) {
                [results addObject:[floorDic objectForKey:@"result"]];
            }
        }
        [self.chekckProcesses addObject:process];
        [self.checkResults addObject:results];
    }
    [self.mainTable reloadData];
}

#pragma mark - end
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
